class StocksController < ApplicationController
  before_filter :must_be_admin
  def own (stock)
  	@ownership = Ownership.new
	@ownership.user_id = 1
	@ownership.stock = stock
	@ownership.bought_at = stock.market_value
	@ownership.quantity = stock.quantity
	@ownership.save!
  end
  # GET /stocks
  # GET /stocks.xml
  def index
    @stocks = Stock.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @stocks }
    end
  end

  # GET /stocks/1
  # GET /stocks/1.xml
  def show
    @stock = Stock.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @stock }
    end
    
  rescue
  	not_existing(id, 'stocks')
  end
 
  def open
	  stock = Stock.find(params[:id])
	  status = Status.first(  :conditions => "stock_id = #{stock.id}" )
	  if status.nil?
		status = Status.new(:stock_id => stock.id, :open => 1, :open_now => 0)
	  else
	  	status.open = 1
	  end
	  status.save!
	  redirect_to( :controller => 'stocks')
  end

  def close
	  stock = Stock.find(params[:id])
	  status = Status.first(  :conditions => "stock_id = #{stock.id}" )
	  status.open = 0
	  status.save!
	  redirect_to( :controller => 'stocks')
  end
  # GET /stocks/new
  # GET /stocks/new.xml
  def new
    @stock = Stock.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @stock }
    end
  end

  # GET /stocks/1/edit
  def edit
    @stock = Stock.find(params[:id])
  rescue
  	not_existing(id, 'stocks')
  end

  # POST /stocks
  # POST /stocks.xml
  def create
    @stock = Stock.new(params[:stock])
    @stock.days_opening = @stock.market_value
    respond_to do |format|
      if @stock.save!
        flash[:notice] = 'Stock was successfully created.'
        format.html { redirect_to(@stock) }
        format.xml  { render :xml => @stock, :status => :created, :location => @stock }
        own (@stock)
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @stock.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /stocks/1
  # PUT /stocks/1.xml
  def update
    @stock = Stock.find(params[:id])
	#@ownership = Ownership.new
	#@ownership.user_id = 1
	#@ownership.stock_id = @stock.id
	#@ownership.quantity = @stock.quantity
    respond_to do |format|
      if @stock.update_attributes(params[:stock])
        flash[:notice] = 'Stock was successfully updated.'
        format.html { redirect_to(@stock) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @stock.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /stocks/1
  # DELETE /stocks/1.xml
  def destroy
    @stock = Stock.find(params[:id])
    @stock.destroy

    respond_to do |format|
      format.html { redirect_to(stocks_url) }
      format.xml  { head :ok }
    end
    rescue
		not_existing(id, 'stocks')
  end

end
